package partitions;

import hips.gui.ComponentLocation;
import hips.gui.WaitingFrame;
import hips.gui.parameters.ParameterFrame;
import hips.gui.parameters.ParameterListener;
import hips.pixel.PixelValue;
import hips.images.ImagePartitionable;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainFrame extends javax.swing.JFrame implements ParameterListener{

    ImagePartitionable img;
    LinkedList<Listener> ListenerList;

    public MainFrame(ImagePartitionable img) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        initComponents();

        ListenerList = new LinkedList<Listener>();


        ComponentLocation.setLocationCenter(0.25f, 0.5f, this);

        this.img = img;
        WaitingFrame wait = new WaitingFrame();
        wait.setVisible(true);
        wait.dispose();
        tabs.setTitleAt(0, "empty");
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        create = new javax.swing.JButton();
        tabs = new javax.swing.JTabbedPane();
        jLabel1 = new javax.swing.JLabel();
        create1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        create.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        create.setText("create new partition");
        create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createActionPerformed(evt);
            }
        });

        tabs.setFont(new java.awt.Font("Calibri", 0, 18));

        jLabel1.setBackground(new java.awt.Color(246, 246, 246));
        jLabel1.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("no partition has been created yet");
        jLabel1.setOpaque(true);
        tabs.addTab("tab1", jLabel1);

        create1.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        create1.setText("close");
        create1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                create1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(create)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE)
                        .addComponent(create1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabs, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(create)
                    .addComponent(create1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createActionPerformed
        ParameterFrame p = new ParameterFrame(img);
        p.addParameterListener(this);
        setEnabled(false);
    }//GEN-LAST:event_createActionPerformed

    private void create1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_create1ActionPerformed
        for (Listener l: ListenerList){
            l.close();
        }
        img.getImagePlus().close();
        dispose();
    }//GEN-LAST:event_create1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton create;
    private javax.swing.JButton create1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTabbedPane tabs;
    // End of variables declaration//GEN-END:variables

    public void ParametersOk(PixelValue alpha, PixelValue omega, float cindex) {
        setEnabled(true);
        if (tabs.getTabCount() == 1 && tabs.getComponent(0) instanceof JLabel){
            tabs.removeAll();
        }
        ListenerList.add(new Listener(img, tabs, alpha, omega, cindex));
    }

    public void ParametersCancelled() {
        setEnabled(true);
    }

}
